package com.kun.valiant.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kun.valiant.common.core.domain.AjaxResult;
import com.kun.valiant.common.utils.SecurityUtils;
import com.kun.valiant.course.domain.Course;
import com.kun.valiant.course.domain.vo.CourseVO;
import com.kun.valiant.course.mapper.CourseMapper;
import com.kun.valiant.course.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程管理 服务实现类
 * </p>
 *
 * @author fangkun
 * @since 2023-06-06
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    CourseMapper courseMapper;

    @Override
    public List<Course> getCourseList(CourseVO courseVO) {
        return courseMapper.getCourseList(courseVO);
    }

    @Override
    public AjaxResult addCourse(CourseVO courseVO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseVO,course);

        course.setCreateBy(SecurityUtils.getUsername());
        course.setCreateTime(LocalDateTime.now());
        course.setDelFlag(0);

        return save(course) ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
    }

    @Override
    public AjaxResult editCourse(CourseVO courseVO) {

        Course course = new Course();
        BeanUtils.copyProperties(courseVO,course);
        course.setUpdateBy(SecurityUtils.getUsername());
        course.setUpdateTime(LocalDateTime.now());

        course.setCreateTime(null);
        course.setCreateBy(null);
        course.setDelFlag(null);

        return updateById(course) ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Override
    public CourseVO getCourseById(Integer courseId) {

        Course course = getById(courseId);
        CourseVO courseVO = new CourseVO();
        BeanUtils.copyProperties(course,courseVO);
        return courseVO;
    }

    @Override
    public boolean removeCourseIds(Integer[] courseIds) {
        LambdaUpdateWrapper<Course> uq = new LambdaUpdateWrapper<>();
        uq.set(Course::getDelFlag,1).in(Course::getCourseId,courseIds);
        return update(uq);
    }

    @Override
    public AjaxResult getCourseByType(Integer type) {

        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Course::getType,type);
        List<Course> courseList = list(wrapper);
        return AjaxResult.success(courseList);
    }
}
